from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.generics import ListAPIView


from apps.goods.models import GoodsCategory


class CategoriesSerializer(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = ['id', 'name']


class CategoriesView(ListAPIView):
    permission_classes = [IsAdminUser]
    serializer_class = CategoriesSerializer

    def get_queryset(self):
        pk = self.kwargs.get('pk')
        return GoodsCategory.objects.filter(parent_id=pk).values('id', 'name')
